package m.olive.data.controller;

import java.util.List;

import m.framework.controller.BaseController;
import m.framework.controller.Task;
import m.framework.controller.validate.Validate;
import m.framework.dao.selector.PageRecord;
import m.framework.dao.selector.Query;
import m.olive.data.model.DatasourceInfo;
import m.olive.data.model.DsParam;
import m.olive.data.model.DsParamDto.ParamType;
import m.olive.data.service.DatasourceInfoService;

import org.springframework.stereotype.Controller;

@Controller
@Task("数据源")
public class DatasourceInfoController extends BaseController {
	
	@Task("数据源首页")
	public DatasourceInfo index(DatasourceInfo datasourceInfo) {
		return datasourceInfo;
	}
	
	@Task("准备新增数据源")
	public DatasourceInfo prepareInsert(DatasourceInfo datasourceInfo) {
		return datasourceInfo;
	}
	
	@Task("准备修改数据源")
	public DatasourceInfo prepareUpdate(DatasourceInfo datasourceInfo) {
		DatasourceInfoService ds=this.getService(DatasourceInfoService.class);
		return ds.getDatasourceInfo(datasourceInfo.getDatasourceInfoId());
	}

	@Task("新增数据源")
	public DatasourceInfo insert(DatasourceInfo datasourceInfo,List<DsParam> param) {
		DatasourceInfoService s=this.getService(DatasourceInfoService.class);
		datasourceInfo.setPk();
		Validate.validate(datasourceInfo);
		for(int i=0;i<param.size();i++){
			DsParam d=param.get(i);
			d.setPk();
			d.setDatasourceInfoId(datasourceInfo.getDatasourceInfoId());
			d.setParamIndex(i);
			d.setParamType(ParamType.出参);
			Validate.validate(d);
		}
		
		s.insertDatasourceInfo(datasourceInfo,param);
		
		return datasourceInfo;
	}
	
	@Task("删除数据源")
	public DatasourceInfo delete(DatasourceInfo datasourceInfo) {
		DatasourceInfoService datasourceInfoService=this.getService(DatasourceInfoService.class);
		datasourceInfoService.deleteDatasourceInfo(datasourceInfo);
		return datasourceInfo;
	}
	
	@Task("修改数据源")
	public DatasourceInfo update(DatasourceInfo datasourceInfo,List<DsParam> param) {
		DatasourceInfoService datasourceInfoService=this.getService(DatasourceInfoService.class);
		datasourceInfoService.updateDatasourceInfo(datasourceInfo,param);
		return datasourceInfo;
	}

	@Task("查询数据源")
	public PageRecord query(Query<DatasourceInfo> query,PageRecord pageRecord){
		return this.defultService().queryPage(query, 1, pageRecord.getRowsPerPage());
	}
	
	@Task("翻页查询数据源")
	public PageRecord queryContinue(PageRecord pageRecord){
		return super.queryContinue(pageRecord);
	}

}
